import * as types from "./mutations-types"
import {
  auth,
  getUserInfo,
  getVideoCitys,
  getVideoCity,
  getCompanyDetailByUserId,
  getGeoJson
} from "@/api"

import { cacheGeoJson, removeToken } from "@/utils/storage"
import genRouterMap from "@/router/router"

// 登录
export const LoginByUsername = ({ commit }, userInfo) => {
  const params = {
    ...userInfo
  }
  return new Promise((resolve, reject) => {
    auth(params)
      .then(res => {
        if (res.status !== 0) {
          reject(res)
        } else {
          const { result } = res
          commit(types.SET_TOKEN, "Bearer " + result)
          getGeoJson("100000_full").then(result => {
            const { data } = result
            cacheGeoJson(100000, data)
          })
          resolve(res)
        }
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 用户角色
 * ROLE_PHT 平和堂账号 （访问admin相关）
 * ROLE_USER 企业用户（访问user相关）
 * ROLE_PLATFORM 代理账号 （访问user相关）
 * ROLE_GOV 政府（访问user相关）
 *
 * ROLE_OPERATION 运营账号（访问admin）
 * ROLE_ADMIN 管理员（访问admin相关）
 * ROLE_DEMO 演示账号（访问admin相关） 对应路由 DEMO_MAP
 */



// 拉取用户信息
export const getUserInfoActions = ({ commit }) => {
  return new Promise((resolve, reject) => {
    getUserInfo()
      .then(res => {
        if (res.code === 200) {
          const result = res.data
          const { rname } = result
          commit(types.SET_USERINFO, result)
          const router = genRouterMap(rname)
          if (router.length === 0) {
            reject(rname)
          } else {
            commit(types.SET_ROUTERMAP, router)
            resolve(result)
          }
        } else {
          reject(res)
        }
      })
      .catch(err => {
        reject(err)
      })
  })
}

// 如果 ROLE_USER 就需要单独拉取用户信息
export const getCompanyInfo = ({ commit, state }) => {
  return new Promise(resolve => {
    const { id } = state.userInfo
    getCompanyDetailByUserId({ userId: id }).then(res => {
      if (res.code == 200 && !!res.data) {
        commit(types.SET_COMPANY_INFO, res.data)
        resolve()
      }
    })
  })
}

// 登出
export const logout = ({ commit }) => {
  return new Promise((resolve, reject) => {
    try {
      commit(types.SET_TOKEN, "")
      removeToken()
      resolve()
    } catch (e) {
      reject(e)
    }
  })
}

const makeCitysPosition = citys => {
  let newArr = []
  citys.forEach(item => {
    if (item.name === "北京") {
      newArr.push({
        name: "北京市",
        value: 40
      })
    } else {
      newArr.push({
        name: item.name,
        value: 40
      })
    }
  })
  return newArr
}

export const getCitys = ({ commit }, citys) => {
  return new Promise((resolve, reject) => {
    getVideoCitys().then(res => {
      if (res.code != 200) reject(res.message)
      else {
        commit("SET_CITYSPO", makeCitysPosition(res.data))
        commit("SET_CITYS", [...res.data])
        resolve()
      }
    })
  })
}

export const getSingleCity = ({ commit }, cityId) => {
  return new Promise((resolve, reject) => {
    getVideoCity(cityId).then(res => {
      if (res.code != 200) reject(res.message)
      else {
        commit("SET_CITY_INFO", res.data)
        resolve(res.data)
      }
    })
  })
}
